package com.czp.person.mapper.impl;

import java.util.List;

import org.apache.ibatis.session.RowBounds;

import com.czp.framework.mapper.BaseMapperSupport;
import com.czp.person.mapper.IPersonMapper;
import com.czp.person.model.Person;
import com.czp.user.model.User;

public class PersonMapperImpl extends BaseMapperSupport implements IPersonMapper {
	
	public List<Person> getPersonList(Person person, RowBounds bounds) throws Exception {
		return this.getSqlSession().selectList(
				"com.czp.person.mapper.IPersonMapper.getPersonList", person, bounds);
	}
	
	public int getPersonRecords(Person person) throws Exception {
		return this.getSqlSession().selectOne(
				"com.czp.person.mapper.IPersonMapper.getPersonRecords", person);
	}

	public int insertPerson(Person person) throws Exception {
		return this.getSqlSession().insert(
				"com.czp.person.mapper.IPersonMapper.insertPerson", person);
	}

	public int updatePerson(Person person) throws Exception {
		return this.getSqlSession().update(
				"com.czp.person.mapper.IPersonMapper.updatePerson", person);
	}
	
	public int deletePerson(Person person, User userId) throws Exception {
		int count = this.getSqlSession().update(
				"com.czp.person.mapper.IPersonMapper.deletePerson", person);
		//当该人员下存在用户时，删除用户
		if(count > 0) {
			if(userId.getId() != null && !userId.getId().equals("")) {
				return this.getSqlSession().update(
						"com.czp.user.mapper.IUserMapper.deleteUser", userId);
			}
			return count;
		}
		return count;
	}
	
	public List<Person> findPersonById(String personId) throws Exception {
		return this.getSqlSession().selectList(
				"com.czp.person.mapper.IPersonMapper.findPersonById", personId);
	}
	
	public List<Person> getPersonName(String name) throws Exception {
		return this.getSqlSession().selectList(
				"com.czp.person.mapper.IPersonMapper.getPersonName", name);
	}

}
